// app.js

App({
  globalData: {
    openId:'',
    qrcode:''
  },

  onLaunch: function () {
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力');
    } else {
      wx.cloud.init({
        env: 'cloud1-0gslhb10a860e522',
        traceUser: true,
      });
    }

    // 清楚本地存储
    var category = wx.getStorageSync('exercise_category');
    console.log("exercise category: " + JSON.stringify(category));
    if (category) {
      try {
        wx.removeStorageSync('exercise_category');
      } catch (e) {
        console.error("remove storage[key:exercise_category] fail:", e);
      }
    }

    let that = this;
    // get openId
    wx.cloud.callFunction({
      name: 'qlFunctions',
      config: {
        env: ''
      },
      data: {
        type: 'getOpenId'
      }
    }).then((resp) => {
      console.log("resp:" + JSON.stringify(resp));
      that.globalData.openId = resp.result.openid;
      console.log("openId:" + that.globalData.openId);
    }).catch((e) => {
      console.error("get openId fail", e);
    });

    // get miniprogram qrcode
    wx.cloud.callFunction({
      name: 'qlFunctions',
      config: {
        env: ''
      },
      data: {
        type: 'getMiniProgramCode'
      }
    }).then((resp) => {
      that.globalData.qrcode = resp.result;
    }).catch((e) => {
      console.log(e);
    });
  },

});
